package com.movie.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import com.movie.beans.MemberBeans;

public class MemberDao {
	private Connection road;
	private ResultSet result;
	private PreparedStatement car;
	private DataSource ds;
	
	public MemberDao(){
		try{		
			Context init = new InitialContext();
			ds = (DataSource)init.lookup("java:comp/env/SJHDB");
		}catch(Exception e){
			e.printStackTrace();
			return;		
		}
	}
	public boolean idcheck(){
		boolean result2=false;
		String sql = "SELECT ID FROM MEMBERS WHERE ID=?";
		try{road = ds.getConnection();
			car = road.prepareStatement(sql);
			car.setString(1, "sjh");	
			result = car.executeQuery();
			if(result.equals(1)){
				result2 = true;
			}else{
				result2 =false;
			}
			
		}catch(Exception e){ e.printStackTrace(); }		
		return result2;
	}
	public boolean MemberInsert(MemberBeans member){
		boolean present = false;
		String sql = "INSERT INTO MEMBERS(ID, PASSWORD, MMNAME, PHONE, GENDER, EMAIL, MSTATUS,IMAGE) VALUES (?, ?, ?,?, ?, ?, '1',?)";
		try{
			road = ds.getConnection();
			road.setAutoCommit(false);
			car = road.prepareStatement(sql);
			car.setString(1, member.getId());
			car.setString(2, member.getPwd());
			car.setString(3, member.getName());
			car.setString(4, member.getPhone());
			car.setString(5, member.getGender());
			car.setString(6, member.getEmail());
			car.setString(7, member.getImage());
			int cnt = car.executeUpdate();
			if(cnt !=0){
				present =true;
			}
			road.commit();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(result != null){ try{ result.close();}catch(SQLException e){}	}
			if(car != null){ try{ car.close();}catch(SQLException e){}	}
			if(road != null){ try{ road.close();}catch(SQLException e){}	}
		}
		return present;
					
	}
	public void searchId(){
	
	}
	public void searchPwd(){
		
	}
	public void modifyPwd(){
		
	}
	public boolean loginMember(MemberBeans member){
		boolean present = false;
		String sql = "SELECT ID, PASSWORD, MMNAME, PHONE, GENDER, EMAIL FROM MEMBERS WHERE ID=? AND PASSWORD=?";
		try{
			road = ds.getConnection();
			
			car = road.prepareStatement(sql);
			car.setString(1, member.getId());
			car.setString(2, member.getPwd());
			
			result = car.executeQuery();
			while(result.next()){
				if(result.getNString(1) != null){ present = true; 
					member.setId(result.getNString(1));
					member.setName(result.getNString(2));
					member.setPhone(result.getNString(3));
					member.setGender(result.getNString(4));
					member.setEmail(result.getNString(5));
				}
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(result != null){ try{ result.close();}catch(SQLException e){}	}
			if(car != null){ try{ car.close();}catch(SQLException e){}	}
			if(road != null){ try{ road.close();}catch(SQLException e){}	}
		}
		return present;
	}
	public void MemberInfo(){
		
	}
	public void ModifyMember(){
		
	}
	public void DeleteMember(){
		
	}
}
